package cn.stronglink.assetmanage.business.patrol.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.PatrolDetail;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.listener.onRecyclerListener;

public class PatrolDetailAdapter extends RecyclerView.Adapter<PatrolDetailAdapter.ViewHolder>{
    private onRecyclerListener mListener;
    private Context mContext;
    private List<SearchAsset> mSearchList;

    private LayoutInflater mLayoutInflater;

    public PatrolDetailAdapter(Context context, List<SearchAsset> searchAssetList){
        this.mContext = context;
        this.mSearchList = searchAssetList;
        this.mLayoutInflater = LayoutInflater.from(context);
    }

    public void setListener(onRecyclerListener listener){
        mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mLayoutInflater.inflate(R.layout.item_patrol_detail_list,
                parent,false);
        ViewHolder viewHolder = new ViewHolder(view,mListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SearchAsset searchAsset = mSearchList.get(position);
        holder.bindBean(searchAsset,position);

        if (position%2 != 0){
            holder.linearLayout.setBackgroundColor(mContext.getResources().
                    getColor(R.color.gray_check_item));
        }else {
            holder.linearLayout.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mSearchList == null ? 0 : mSearchList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        private TextView tv_num,tv_asset_num,tv_asset_name,tv_asset_rfid,tv_asset_area,
                tv_asset_sn;
        private ImageView tv_asset_picture;
        private onRecyclerListener mListener;

        private LinearLayout linearLayout;
        public ViewHolder(View itemView, onRecyclerListener listener) {
            super(itemView);
            tv_num = itemView.findViewById(R.id.item_patrol_detail_num);
            tv_asset_num = itemView.findViewById(R.id.item_patrol_detail_asset_num);
            tv_asset_name = itemView.findViewById(R.id.item_patrol_detail_asset_name);
            tv_asset_rfid = itemView.findViewById(R.id.item_patrol_detail_rfid);
            tv_asset_area = itemView.findViewById(R.id.item_patrol_detail_area);
            tv_asset_sn = itemView.findViewById(R.id.item_patrol_detail_sn);
            tv_asset_picture = itemView.findViewById(R.id.item_patrol_detail_picture);
            linearLayout = itemView.findViewById(R.id.item_patrol_detail);
            mListener = listener;
            itemView.setOnClickListener(this);
        }
        public void bindBean(SearchAsset searchAsset,int position){
            tv_num.setText(position+1 + "");
            tv_asset_num.setText(searchAsset.getAssetNo());
            tv_asset_name.setText(searchAsset.getAssetName());
            tv_asset_rfid.setText(searchAsset.getRfidNo());
            tv_asset_area.setText(searchAsset.getRoomName());
            tv_asset_sn.setText(searchAsset.getSerialNo());
            String url = searchAsset.getPictureUri();
            if (url != null) {
                tv_asset_picture.setVisibility(View.VISIBLE);
                RequestOptions options = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.color.color_f6)
                        .diskCacheStrategy(DiskCacheStrategy.ALL);
                Glide.with(mContext)
                        .load(url)
                        .apply(options)
                        .into(tv_asset_picture);
            }else{
                tv_asset_picture.setVisibility(View.VISIBLE);
            }
        }
        @Override
        public void onClick(View v) {
            if (mListener != null) {
                mListener.onItemClicked(v, getAdapterPosition());
            }
        }
    }

    public void notifyDatasChanged(List<SearchAsset> data){
        this.mSearchList = data;
        this.notifyDataSetChanged();
    }
}
